package cuongnh.project.japanesediary.vocabulary.dao;

import java.util.Date;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import cuongnh.project.japanesediary.vocabulary.model.Sentence;

@Transactional
public class SentenceDao extends HibernateDaoSupport {

	public void save(Sentence a1) {
		a1.setCreatedDate(new Date());
		a1.setModifiedDate(new Date());
		getHibernateTemplate().save(a1);
	}
	
	public void saveOrUpdate(Sentence a1) {
		a1.setModifiedDate(new Date());
		if(a1.getCreatedDate() == null){
			a1.setCreatedDate(new Date());
		}
		getHibernateTemplate().saveOrUpdate(a1);
	}

	/**
	 * 'load' returns a proxy with only the id populated. To access members in
	 * the proxy requires a Hibernate session. Haven't figured out how to do
	 * transactions between bundles yet...
	 */
	public Sentence load(long id) {
		return (Sentence) getHibernateTemplate().load(Sentence.class, id);
	}

	public Sentence get(long id) {
		return (Sentence) getHibernateTemplate().get(Sentence.class, id);
	}

	public void update(Sentence a1) {
		a1.setModifiedDate(new Date());
		getHibernateTemplate().update(a1);
	}

	public void delete(Sentence a1) {
		getHibernateTemplate().delete(a1);
		// getHibernateTemplate().
	}

	public Sentence merge(Sentence a1) {
		a1.setModifiedDate(new Date());
		return (Sentence) getHibernateTemplate().merge(a1);
	}
	
	public Sentence getSentenceWithCategories(long sentenceId) {
		Sentence sentence = getHibernateTemplate().get(Sentence.class,
				sentenceId);
		getHibernateTemplate().initialize(sentence.getSentenceCategories());
		return sentence;
	}
	
	public Sentence getSentenceWithVocabularies(long sentenceId) {
		Sentence sentence = getHibernateTemplate().get(Sentence.class,
				sentenceId);
		getHibernateTemplate().initialize(sentence.getVocabularies());
		return sentence;
	}

	public List<Sentence> getAll() {
		return getHibernateTemplate().loadAll(Sentence.class);
	}

}
